package com.example.thecharacterbook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.net.URI;

public class ProviderText01 extends AppCompatActivity {
    private EditText word,mean,example,example2,example3;
    private Button button,button_Info,button_UpDate,button_Delete;
    MyContentProvider provider;
    ContentValues values = new ContentValues();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider_text01);
        provider = new MyContentProvider();
        word=findViewById(R.id.word);
        mean=findViewById(R.id.mean);
        example=findViewById(R.id.example);
        example2=findViewById(R.id.example2);
        example3=findViewById(R.id.example3);
        button=findViewById(R.id.AddBtn);
        button_Delete=findViewById(R.id.DeleteBtn);
        button_Info=findViewById(R.id.InfoBtn);
        button_UpDate=findViewById(R.id.UPDATEBtn);
        button_UpDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String [] b = new String[1];
                String YourInput = word.getText().toString();
                if(YourInput.equals(null))
                {
                    b[0] ="";
                }
                else
                    b[0]=YourInput;
                values.put("TheWord",word.getText().toString().trim());
                values.put("TheWord_Mean",mean.getText().toString().trim());
                values.put("Phrase",example.getText().toString().trim());
                values.put("Phrase2",example2.getText().toString().trim());
                values.put("Phrase3",example3.getText().toString().trim());
                int sum = getContentResolver().update(MyContentProvider.CONTENT_URI_1,values,"TheWord = ? ",b);
                Toast.makeText(ProviderText01.this,"更新成功,",Toast.LENGTH_SHORT).show();

            }
            });

        button_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ContentValues updateValues = new ContentValues();
                String [] b = new String[1];
                String YourInput = word.getText().toString();
                if(YourInput.equals(null))
                {
                    b[0] ="";
                }
                else
                    b[0]=YourInput;
                int sum = getContentResolver().delete(MyContentProvider.CONTENT_URI_1,"TheWord = ? ",b);
                Toast.makeText(ProviderText01.this,"删除成功,共有"+sum+"行",Toast.LENGTH_SHORT).show();
            }
        });
        button_Info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String [] a={"TheWord"};
                String [] b = new String[1];
                String YourInput = word.getText().toString();
                if(YourInput.equals(null))
                {
                    b[0] ="";
                }
                else
                b[0]=YourInput;
                Cursor cr = getContentResolver().query(MyContentProvider.CONTENT_URI_1,null,"TheWord = ? ",b,null);
                StringBuilder stringBuilder = new StringBuilder();
                if(cr==null){
                    Toast.makeText(ProviderText01.this,"Error",Toast.LENGTH_SHORT).show();
                }
                else
                while(cr.moveToNext()){
                   String ID =cr.getString(0);
                   String word =cr.getString(1);
                   String mean =cr.getString(2);
                   String e1 =cr.getString(3);
                   String e2 =cr.getString(4);
                   String e3 =cr.getString(5);
                   // stringBuilder.append(ID+" "+word+" "+mean);
                   stringBuilder.append(ID+" "+word+" "+mean+" "+e1+" "+e2+" "+e3+" ");
                }
                Toast.makeText(ProviderText01.this,stringBuilder.toString(),Toast.LENGTH_SHORT).show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                values.put("TheWord",word.getText().toString().trim());
                values.put("TheWord_Mean",mean.getText().toString().trim());
                values.put("Phrase",example.getText().toString().trim());
                values.put("Phrase2",example2.getText().toString().trim());
                values.put("Phrase3",example3.getText().toString().trim());
               Uri uri = getContentResolver().insert(MyContentProvider.CONTENT_URI_1,values);
                Toast.makeText(ProviderText01.this,"插入成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
}